package dwl.graduation.design.controller;


import dwl.graduation.design.domain.MenuInsertRequest;
import dwl.graduation.design.domain.MenuUpdateRequest;
import dwl.graduation.design.domain.PermissionInsertRequest;
import dwl.graduation.design.domain.PermissionUpdateRequest;
import dwl.graduation.design.service.MenuService;
import dwl.graduation.design.utils.MessageConstant;
import dwl.graduation.design.utils.PageResult;
import dwl.graduation.design.utils.QueryPageBean;
import dwl.graduation.design.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping(path = "/menu")
public class MenuController {

    private final MenuService menuService;

    @Autowired
    public MenuController(MenuService menuService){
        this.menuService = menuService;
    }

    @RequestMapping(value = "/findPermissionList", method = RequestMethod.POST)
    public PageResult findMenuList(@RequestBody QueryPageBean queryPageBean) {
        return menuService.findMenuList(queryPageBean);
    }

    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    public Result insertPermission(@RequestBody MenuInsertRequest request) {
        try {
            boolean flag = menuService.insertMenu(request);
            if (flag) {
                return new Result(true, MessageConstant.ADD_MENU_SUCCESS);
            } else {
                return new Result(false, MessageConstant.ADD_MENU_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.ADD_MENU_FAIL);
        }
    }

    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public Result deleteMenu(@RequestParam("id") Integer id) {
        try {
            menuService.deleteMenu(id);
            return new Result(true, MessageConstant.DELETE_MENU_SUCCESS);
        } catch (Exception e) {
            return new Result(false, MessageConstant.DELETE_MENU_FAIL);
        }
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result updateMenu(@RequestBody MenuUpdateRequest roleUpdateRequest) {
        try {
            boolean flag = menuService.updateMenu(roleUpdateRequest);
            if(flag){
                return new Result(true, MessageConstant.EDIT_MENU_SUCCESS);
            }else{
                return new Result(false, MessageConstant.EDIT_MENU_FAIL);
            }
        } catch (Exception e) {
            return new Result(false, MessageConstant.EDIT_MENU_FAIL);
        }
    }


}
